package com.cljz.makepassenger.present.activity;

import com.cljz.makepassenger.base.BaseP;
import com.cljz.makepassenger.base.Constant;
import com.cljz.makepassenger.model.net.api.Api;
import com.cljz.makepassenger.model.net.bean.ResponseBean;
import com.cljz.makepassenger.ui.activity.PhoneBindingActivity;
import com.clzj.makepassenger.bean.UserInfo;
import com.google.gson.Gson;

import java.util.HashMap;

import cn.droidlover.xdroidmvp.net.ApiSubcriber;
import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.net.XApi;
import cn.droidlover.xdroidmvp.net.diy.Base64Utils;
import okhttp3.RequestBody;

/**
 * Created by Administrator on 2017/4/10 0010.
 */

public class PhoneBindingP extends BaseP<PhoneBindingActivity> {
    /**
     * 获取验证码
     * @param
     */
    public void getSms() {
        Gson gson = new Gson();
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("token", getToken());
        paramsMap.put("send_type", Constant.SmsType.SMS_REPLACE_PHONE);
        String body = Base64Utils.enCode(gson.toJson(paramsMap));
        RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("text/plain;charset=UTF-8"), body);
        Api.getResponseInfoAPI().getSmsSendData(requestBody)
                .compose(XApi.<ResponseBean>getApiTransformer())         //异常处理
                .compose(XApi.<ResponseBean>getScheduler())              //线程切换
                .compose(getV().<ResponseBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<ResponseBean>() {
                    @Override
                    protected void onFail(NetError error) {
                    }

                    @Override
                    public void onNext(ResponseBean smsSendBean) {
                        if ("0".equals(smsSendBean.getError_code())) {
                            getV().getSmsSucceed();
                        } else if ("1000".equals(smsSendBean.getError_code())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().getSmsFail(smsSendBean.getMsg());
                        }
                    }
                });

    }
    public void changeBySms(final String phone, String code) {
        Gson gson = new Gson();
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("token",getToken());
        paramsMap.put("phone", phone);
        paramsMap.put("verify_code", code);
        String body = Base64Utils.enCode(gson.toJson(paramsMap));
        final RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("text/plain;charset=UTF-8"), body);
        Api.getResponseInfoAPI().getReplacePhoneData(requestBody)
                .compose(XApi.<ResponseBean>getApiTransformer())
                .compose(XApi.<ResponseBean>getScheduler())
                .compose(getV().<ResponseBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<ResponseBean>() {
                    @Override
                    protected void onFail(NetError error) {
                    }
                    //修改成功接下来要做的事情
                    @Override
                    public void onNext(ResponseBean responseBean) {
                        if ("0".equals(responseBean.getError_code())) {
                            //L.e(token);
                            initDB(phone);
                            getV().changeSucceed(responseBean);
                        } else if ("1000".equals(responseBean.getError_code())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().changeFail(responseBean.getMsg());
                        }
                    }
                });
    }

    private void initDB(String phone) {
        StringBuilder stringBuilder = new StringBuilder(phone);
        stringBuilder.replace(3,7,"****");
        UserInfo userInfo = getLocalUserInfo(getToken());
        userInfo.setUser_phone(stringBuilder.toString());
        daoSession.getUserInfoDao().update(userInfo);
    }
}
